package com.meihua.shellapp.fragment;


import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.SearchActivity;
import com.meihua.shellapp.adapter.GoodsViewPagerAdapter;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.ProductTypeBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.URL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 产品中心
 */
public class GoodsFragment extends BaseFragment {
    @Bind(R.id.top_rl_search)
    RelativeLayout mRlSearch;
    @Bind(R.id.goods_tabs)
    TabLayout mTabs;
    @Bind(R.id.goods_vpager)
    ViewPager mViewpager;
    private GoodsViewPagerAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_goods;
    }

    @Override
    protected void initView() {
        mRlSearch.setVisibility(View.VISIBLE);
        getServerData();
    }

    /*获取一级数据*/
    private void getServerData() {
        Map<String, String> params = new HashMap();
        NetHelper.post(URL.getUrl().PRODUCT_TYPE, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                ProductTypeBean bean = MyApplication.getInstance().getBeanForJson(ProductTypeBean.class, response.body());
                if (bean == null) return;
                if (bean.getData().size() != 0) {
                    List<ProductTypeBean.DataBean> data = bean.getData();
                     /*设置tab+vp*/
                    if (adapter == null) {
                        adapter = new GoodsViewPagerAdapter(getChildFragmentManager(), data);
                        mViewpager.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                    mTabs.setupWithViewPager(mViewpager);
                }
            }
        }, true);
    }

    @OnClick(R.id.top_rl_search)
    public void onViewClicked() {
        /*搜索中心界面*/
        startActivity(new Intent(getActivity(), SearchActivity.class));
    }
}
